#include "../TestDefinition.h"
#include "PemCodec.h"

PTF_TEST_CASE(PemEncodingTest)
{
	// Encode short vector
	{
		std::vector<uint8_t> data = { 0x10, 0x11, 0x12 };
		PTF_ASSERT_EQUAL(pcpp::PemCodec::encode(data, "LABEL"), "-----BEGIN LABEL-----\nEBES\n-----END LABEL-----\n");
	}

	// Encode long vector
	{
		std::vector<uint8_t> data = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
			                          0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
			                          0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
			                          0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34,
			                          0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41,
			                          0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A };
		std::string expectedPem =
		    "-----BEGIN LABEL-----\nAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8w\nMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUo=\n-----END LABEL-----\n";
		PTF_ASSERT_EQUAL(pcpp::PemCodec::encode(data, "LABEL"), expectedPem);
	}

	// Invalid arguments
	{
		PTF_ASSERT_RAISES(pcpp::PemCodec::encode({ 0x10, 0x11, 0x12 }, ""), std::invalid_argument,
		                  "PEM label cannot be empty");
		PTF_ASSERT_RAISES(pcpp::PemCodec::encode({}, "LABEL"), std::invalid_argument, "PEM data cannot be empty");
	}
}

PTF_TEST_CASE(PemDecodingTest)
{
	// Decode a public key
	{
		std::string pem =
		    "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArkF7tOmIYqz7dST6kX6F\n8c9jB+DC7L5qvHApTyHyADTwXCNT4o3xppuzKOI5mlG8VK0yei4BIkUJXBfzts0j\nH7s5cQccpK0K4yOfoR2Isry43xwrchZnO6MAjH01/J7fMYwdKJkGdlUlAbFgYN2/\nyd5IK2SDAAPLWS4KzViR3TZn5leWBdrGyepAN31sONOBlxZ96pnF5BsCmokmVvct\nkocb8LKMMrDmSLchoOIzrrLaatYNmIM3uRkQWY0JpsPsA8zQLaDqsEO77JaSg2P9\nsfWF8CJOEVHQEFzbExXhsFzkb0Pr57G3PMkDmaR3aY/x/6Wsk8T1IkClnp84WFMQ\nGQIDAQAB\n-----END PUBLIC KEY-----\n";
		std::vector<uint8_t> expectedData = {
			0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
			0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xae, 0x41, 0x7b,
			0xb4, 0xe9, 0x88, 0x62, 0xac, 0xfb, 0x75, 0x24, 0xfa, 0x91, 0x7e, 0x85, 0xf1, 0xcf, 0x63, 0x07, 0xe0, 0xc2,
			0xec, 0xbe, 0x6a, 0xbc, 0x70, 0x29, 0x4f, 0x21, 0xf2, 0x00, 0x34, 0xf0, 0x5c, 0x23, 0x53, 0xe2, 0x8d, 0xf1,
			0xa6, 0x9b, 0xb3, 0x28, 0xe2, 0x39, 0x9a, 0x51, 0xbc, 0x54, 0xad, 0x32, 0x7a, 0x2e, 0x01, 0x22, 0x45, 0x09,
			0x5c, 0x17, 0xf3, 0xb6, 0xcd, 0x23, 0x1f, 0xbb, 0x39, 0x71, 0x07, 0x1c, 0xa4, 0xad, 0x0a, 0xe3, 0x23, 0x9f,
			0xa1, 0x1d, 0x88, 0xb2, 0xbc, 0xb8, 0xdf, 0x1c, 0x2b, 0x72, 0x16, 0x67, 0x3b, 0xa3, 0x00, 0x8c, 0x7d, 0x35,
			0xfc, 0x9e, 0xdf, 0x31, 0x8c, 0x1d, 0x28, 0x99, 0x06, 0x76, 0x55, 0x25, 0x01, 0xb1, 0x60, 0x60, 0xdd, 0xbf,
			0xc9, 0xde, 0x48, 0x2b, 0x64, 0x83, 0x00, 0x03, 0xcb, 0x59, 0x2e, 0x0a, 0xcd, 0x58, 0x91, 0xdd, 0x36, 0x67,
			0xe6, 0x57, 0x96, 0x05, 0xda, 0xc6, 0xc9, 0xea, 0x40, 0x37, 0x7d, 0x6c, 0x38, 0xd3, 0x81, 0x97, 0x16, 0x7d,
			0xea, 0x99, 0xc5, 0xe4, 0x1b, 0x02, 0x9a, 0x89, 0x26, 0x56, 0xf7, 0x2d, 0x92, 0x87, 0x1b, 0xf0, 0xb2, 0x8c,
			0x32, 0xb0, 0xe6, 0x48, 0xb7, 0x21, 0xa0, 0xe2, 0x33, 0xae, 0xb2, 0xda, 0x6a, 0xd6, 0x0d, 0x98, 0x83, 0x37,
			0xb9, 0x19, 0x10, 0x59, 0x8d, 0x09, 0xa6, 0xc3, 0xec, 0x03, 0xcc, 0xd0, 0x2d, 0xa0, 0xea, 0xb0, 0x43, 0xbb,
			0xec, 0x96, 0x92, 0x83, 0x63, 0xfd, 0xb1, 0xf5, 0x85, 0xf0, 0x22, 0x4e, 0x11, 0x51, 0xd0, 0x10, 0x5c, 0xdb,
			0x13, 0x15, 0xe1, 0xb0, 0x5c, 0xe4, 0x6f, 0x43, 0xeb, 0xe7, 0xb1, 0xb7, 0x3c, 0xc9, 0x03, 0x99, 0xa4, 0x77,
			0x69, 0x8f, 0xf1, 0xff, 0xa5, 0xac, 0x93, 0xc4, 0xf5, 0x22, 0x40, 0xa5, 0x9e, 0x9f, 0x38, 0x58, 0x53, 0x10,
			0x19, 0x02, 0x03, 0x01, 0x00, 0x01
		};
		auto decodedData = pcpp::PemCodec::decode(pem, "PUBLIC KEY");
		PTF_ASSERT_TRUE(decodedData == expectedData);
	}

	// Decode short data
	{
		std::string pem = "-----BEGIN LABEL-----\nERIT\n-----END LABEL-----\n";
		std::vector<uint8_t> expectedData = { 0x11, 0x12, 0x13 };
		auto decodedData = pcpp::PemCodec::decode(pem);
		PTF_ASSERT_TRUE(decodedData == expectedData);
	}

	// Invalid data
	{
		PTF_ASSERT_RAISES(pcpp::PemCodec::decode(""), std::invalid_argument, "Missing BEGIN or END in PEM data");
		PTF_ASSERT_RAISES(pcpp::PemCodec::decode("foo"), std::invalid_argument, "Missing BEGIN or END in PEM data");
		PTF_ASSERT_RAISES(pcpp::PemCodec::decode("-----BEGIN FOO-----\nERIT\n-----END FOO-----\n", "LABEL"),
		                  std::invalid_argument, "Unexpected BEGIN label in PEM - expected 'LABEL' but got 'FOO'");
		PTF_ASSERT_RAISES(pcpp::PemCodec::decode("-----BEGIN LABEL-----\nabc\n-----BEGIN LABEL-----\n"),
		                  std::invalid_argument, "Unexpected BEGIN while already inside a PEM block");
		PTF_ASSERT_RAISES(pcpp::PemCodec::decode("-----BEGIN -----"), std::invalid_argument,
		                  "Invalid BEGIN label in PEM");
		PTF_ASSERT_RAISES(pcpp::PemCodec::decode("-----BEGIN foo\nbar"), std::invalid_argument,
		                  "Invalid BEGIN suffix in PEM");
		PTF_ASSERT_RAISES(pcpp::PemCodec::decode("-----END LABEL-----\nfoo"), std::invalid_argument,
		                  "END found before BEGIN in PEM");
		PTF_ASSERT_RAISES(pcpp::PemCodec::decode("-----BEGIN L1-----\nERIT\n-----END L2-----\n"), std::invalid_argument,
		                  "BEGIN and END labels do not match in PEM");
		PTF_ASSERT_RAISES(pcpp::PemCodec::decode("-----BEGIN LABEL-----\n-----END LABEL-----\n"), std::invalid_argument,
		                  "No base64 content found in PEM data");
		PTF_ASSERT_RAISES(pcpp::PemCodec::decode("-----BEGIN LABEL-----\nfoo\n-----END LABEL-----\n"),
		                  std::invalid_argument, "Invalid base64 encoded data - Size not divisible by 4");
	}
}
